﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System;
using System;
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Text;
using netDxf;
using netDxf.Blocks;
using netDxf.Collections;
using netDxf.Entities;
using netDxf.Header;
using netDxf.Objects;
using netDxf.Tables;
using netDxf.Units;
using Attribute = netDxf.Entities.Attribute;
using Image = netDxf.Entities.Image;
using Point = netDxf.Entities.Point;
using Trace = netDxf.Entities.Trace;
using System.Threading;

namespace TestDxfDocument
{

    class Tapered_reamerindexableinserts
    {
        public static void _Tapered_reamerindexableinserts(Dictionary<string, Object> data)//细柄丝锥
        {
            DxfDocument dxf = new DxfDocument();
            //图层
            Layer contour = new Layer("contour") { Color = AciColor.Cyan };    //轮廓层
            Layer dimensioning = new Layer("dimensioning");    //尺寸层
            Layer refin = new Layer("refin") { Color = AciColor.Red };    //基准线/内轮廓
            refin.Linetype = Linetype.Dashed;
            Layer centre = new Layer("centre") { Color = AciColor.Yellow };    //中心线
            Layer textlayer = new Layer("textlayer") { Color = AciColor.Blue };    //文本层
            Layer hatching = new Layer("hatching") { Color = AciColor.Blue };    //填充层
            centre.Linetype = Linetype.DashDot;
            Layer additionalline = new Layer("additionalline") { Color = AciColor.Cyan };    //附加线
            Layer cutting = new Layer("cutting") { Color = AciColor.Red };    //切削层
            Layer nocutting = new Layer("nocutting");    //非切削层
            Layer skcontour = new Layer("skcontour") { Color = AciColor.Cyan };    //sk轮廓层
            Layer skdimensioning = new Layer("skdimensioning");    //sk尺寸线层
            Layer skrefin = new Layer("skrefin") { Color = AciColor.Red };    //sk基准线/内轮廓
            Layer sktext = new Layer("sktext") { Color = AciColor.Blue };  //sk文本
            Layer skhatching = new Layer("skhatching") { Color = AciColor.Blue };    //sk填充
            Layer skadditionlline = new Layer("skadditionlline") { Color = AciColor.Cyan };    //sk附加线
            Layer skviewcontour = new Layer("skviewcontour") { Color = AciColor.Cyan };    //skview轮廓
            Layer skviewdimensioning = new Layer("skviewdimensioning");    //skview尺寸线层

            //参数
            double pi = Math.PI;
            string path = Convert.ToString(data["path"]);

            double scale = 2; //放大系数


            double oal = 46.75;
            double lf = 44.53;
            double dconms = 4.12;
            double lpr = 35.18;
            double ldc = 2.22;
            double dc = 4.96;
            double prfa = 4;




            //坐标
            Vector2 v1 = new Vector2(0, 0);
            Vector2 v2 = new Vector2(v1.X - oal - 4, v1.Y);
            Vector2 v3 = new Vector2(v1.X - 2, v1.Y);
            Vector2 v4 = new Vector2(v3.X - ldc, v3.Y + dc / 2);
            Vector2 v5 = new Vector2(v3.X, v4.Y - ldc * Math.Tan(prfa / 2 * pi / 180));
            Vector2 v6 = new Vector2(v3.X - oal / 2.363, v5.Y + oal / 2.363 * Math.Tan(prfa / 2 * pi / 180));
            Vector2 v7 = new Vector2(v3.X - oal / 2.25, v3.Y + dconms / 2);
            Vector2 v8 = new Vector2(v3.X - oal + 0.5, v7.Y);
            Vector2 v9 = new Vector2(v8.X - 0.5, v8.Y - 0.3);
            Vector2 v10 = new Vector2(v9.X, v9.Y * -1);
            Vector2 v11 = new Vector2(v8.X, v8.Y * -1);
            Vector2 v12 = new Vector2(v7.X, v7.Y * -1);
            Vector2 v13 = new Vector2(v6.X, v3.Y - dc / 1.93);
            Vector2 v14 = new Vector2(v13.X, v6.Y * -1);
            Vector2 v15 = new Vector2(v3.X, v5.Y * -1);
            Vector2 v16 = new Vector2(v15.X, v15.Y + (v13.Y - v14.Y));
            Vector2 v17 = new Vector2(v3.X, v3.Y - dc / 4.313);
            Vector2 v18 = new Vector2(v3.X, v3.Y - dc / 7.086);
            Vector2 v19 = new Vector2(v13.X, v13.Y + (v17.Y - v16.Y));
            Vector2 v20 = new Vector2(v19.X, v19.Y + (v18.Y - v17.Y));
            Vector2 v21 = new Vector2(v20.X, v6.Y - (v5.Y - v3.Y));
            Vector2 v22 = new Vector2(v6.X, v6.Y - dc / 7.188);
            Vector2 v23 = new Vector2(v22.X + oal / 36.523, v22.Y - oal / 36.523 * Math.Tan(prfa / 2 * pi / 180));
            Vector2 v24 = new Vector2(v23.X + oal / 45.833, v23.Y - oal / 45.833 * Math.Tan(prfa / 2 * pi / 180));
            Vector2 v25 = new Vector2(v24.X + oal / 13.997, v24.Y - oal / 13.997 * Math.Tan(prfa / 2 * pi / 180));
            Vector2 v26 = new Vector2(v25.X + oal / 45.833, v25.Y - oal / 45.833 * Math.Tan(prfa / 2 * pi / 180));
            Vector2 v27 = new Vector2(v26.X + oal / 13.997, v26.Y - oal / 13.997 * Math.Tan(prfa / 2 * pi / 180));
            Vector2 v28 = new Vector2(v27.X + oal / 45.833, v27.Y - oal / 45.833 * Math.Tan(prfa / 2 * pi / 180));
            Vector2 v29 = new Vector2(v28.X + oal / 13.997, v28.Y - oal / 13.997 * Math.Tan(prfa / 2 * pi / 180));
            Vector2 v30 = new Vector2(v29.X + oal / 45.833, v29.Y - oal / 45.833 * Math.Tan(prfa / 2 * pi / 180));
            Vector2 v31 = new Vector2(v3.X, v5.Y - (v6.Y - v22.Y));
            Vector2 v32 = new Vector2(v23.X, v23.Y + (v6.Y - v22.Y) / 2);
            Vector2 v33 = new Vector2(v24.X, v24.Y + (v6.Y - v22.Y) / 2);
            Vector2 v34 = new Vector2(v25.X, v25.Y + (v6.Y - v22.Y) / 2);
            Vector2 v35 = new Vector2(v26.X, v26.Y + (v6.Y - v22.Y) / 2);
            Vector2 v36 = new Vector2(v27.X, v27.Y + (v6.Y - v22.Y) / 2);
            Vector2 v37 = new Vector2(v28.X, v28.Y + (v6.Y - v22.Y) / 2);
            Vector2 v38 = new Vector2(v29.X, v29.Y + (v6.Y - v22.Y) / 2);
            Vector2 v39 = new Vector2(v30.X, v30.Y + (v6.Y - v22.Y) / 2);
            Vector2 v40 = new Vector2(v6.X, v6.Y - (v6.Y - v22.Y) / 2);
            Vector2 v41 = new Vector2(v3.X - lpr, v3.Y);
            Vector2 v42 = new Vector2(v41.X, v41.Y + dconms / 2 + 1);
            Vector2 v43 = new Vector2(v42.X, v42.Y * -1);
            Vector2 v44 = new Vector2(v3.X + oal / 31.588, v3.Y + dc / 1.148);
            Vector2 v45 = new Vector2(v44.X - oal / 6.258, v44.Y);
            Vector2 v46 = new Vector2(v45.X, v45.Y * -1);
            Vector2 v47 = new Vector2(v44.X, v46.Y);

            Vector2 v48 = new Vector2(v1.X - oal / 2.095, v1.Y - oal / 1.528);
            Vector2 v49 = new Vector2(v48.X - oal / 5.189, v48.Y);
            Vector2 v74 = new Vector2(v48.X - 1, v48.Y);
            Vector2 v50 = new Vector2(v74.X - oal / 19.809, v74.Y - dc / 1.886);
            Vector2 v51 = new Vector2(v74.X, v50.Y + (v74.X - v50.X) * Math.Tan(prfa / 2 * pi / 180));
            Vector2 v52 = new Vector2(v74.X - oal / 6.159, v51.Y - oal / 6 * Math.Tan(prfa / 2 * pi / 180));
            Vector2 v53 = new Vector2(v74.X, v74.Y + (v74.Y - v51.Y));
            Vector2 v54 = new Vector2(v52.X, v74.Y + (v74.Y - v52.Y));
            Vector2 v55 = new Vector2(v74.X, v74.Y - dc / 2.431);
            Vector2 v56 = new Vector2(v52.X, v52.Y + (v55.Y - v51.Y));
            Vector2 v57 = new Vector2(v74.X, v74.Y - dc / 3.786);
            Vector2 v58 = new Vector2(v56.X, v56.Y + (v57.Y - v55.Y));
            Vector2 v59 = new Vector2(v74.X, v74.Y - dc / 5.905);
            Vector2 v60 = new Vector2(v58.X, v58.Y + (v59.Y - v57.Y));
            Vector2 v61 = new Vector2(v53.X, v53.Y - dc / 7.75);
            Vector2 v62 = new Vector2(v50.X, v61.Y + (v61.X - v50.X) * Math.Tan(prfa / 2 * pi / 180));
            Vector2 v63 = new Vector2(v61.X - oal / 11, v61.Y + oal / 11 * Math.Tan(prfa / 2 * pi / 180));
            Vector2 v64 = new Vector2(v63.X - oal / 37.4, v63.Y + oal / 37.4 * Math.Tan(prfa / 2 * pi / 180));
            Vector2 v65 = new Vector2(v54.X, v64.Y + (v64.X - v54.X) * Math.Tan(prfa / 2 * pi / 180));
            Vector2 v66 = new Vector2(v63.X, v63.Y + (v53.Y - v61.Y) / 2);
            Vector2 v67 = new Vector2(v64.X, v64.Y + (v66.Y - v63.Y));
            Vector2 v68 = new Vector2(v48.X - oal / 4.592, v48.Y + dc / 0.769);
            Vector2 v69 = new Vector2(v68.X + oal / 2.417, v68.Y);
            Vector2 v70 = new Vector2(v69.X, v69.Y - dc / 0.371);
            Vector2 v71 = new Vector2(v68.X, v70.Y);
            Vector2 v72 = new Vector2(v53.X + oal / 37.702, v53.Y - dc / 6.703);
            Vector2 v73 = new Vector2(v72.X + oal / 13.095, v72.Y - dc / 1.56);
            Vector2 v75 = new Vector2(v65.X, v65.Y - (v61.Y - v74.Y));





            //作图
            Line line1 = new Line(v1, v2);
            line1.Layer = centre;
            line1.Linetype = Linetype.DashDot;
            line1.LinetypeScale = 3;
            dxf.AddEntity(line1);

            Line line2 = new Line(v42, v43);
            line2.Layer = centre;
            line2.Linetype = Linetype.DashDot;
            line2.LinetypeScale = 3;
            dxf.AddEntity(line2);

            LwPolyline line3 = new LwPolyline();
            line3.Vertexes.Add(new LwPolylineVertex(v8));
            line3.Vertexes.Add(new LwPolylineVertex(v11));
            line3.Vertexes.Add(new LwPolylineVertex(v10));
            line3.Vertexes.Add(new LwPolylineVertex(v9));
            line3.Vertexes.Add(new LwPolylineVertex(v8));
            line3.Vertexes.Add(new LwPolylineVertex(v7));
            line3.Vertexes.Add(new LwPolylineVertex(v12));
            line3.Vertexes.Add(new LwPolylineVertex(v11));
            fun.PolyFill(dxf, line3, 0.2, contour);

            LwPolyline line4 = new LwPolyline();
            line4.Vertexes.Add(new LwPolylineVertex(v23));
            line4.Vertexes.Add(new LwPolylineVertex(v22));
            line4.Vertexes.Add(new LwPolylineVertex(v40));
            line4.Vertexes.Add(new LwPolylineVertex(v7));
            line4.Vertexes.Add(new LwPolylineVertex(v40));
            line4.Vertexes.Add(new LwPolylineVertex(v6));
            line4.Vertexes.Add(new LwPolylineVertex(v5));
            line4.Vertexes.Add(new LwPolylineVertex(v31));
            line4.Vertexes.Add(new LwPolylineVertex(v30));
            line4.Vertexes.Add(new LwPolylineVertex(v29));
            line4.Vertexes.Add(new LwPolylineVertex(v38));
            line4.Vertexes.Add(new LwPolylineVertex(v39));
            line4.Vertexes.Add(new LwPolylineVertex(v30));
            line4.Vertexes[8].Bulge = Math.Tan(-156 * pi / 180 / 4);
            fun.PolyFill(dxf, line4, 0.2, contour);

            LwPolyline line5 = new LwPolyline();
            line5.Vertexes.Add(new LwPolylineVertex(v29));
            line5.Vertexes.Add(new LwPolylineVertex(v28));
            line5.Vertexes.Add(new LwPolylineVertex(v37));
            line5.Vertexes.Add(new LwPolylineVertex(v36));
            line5.Vertexes.Add(new LwPolylineVertex(v27));
            line5.Vertexes.Add(new LwPolylineVertex(v28));
            line5.Vertexes[4].Bulge = Math.Tan(156 * pi / 180 / 4);
            fun.PolyFill(dxf, line5, 0.2, contour);

            LwPolyline line6 = new LwPolyline();
            line6.Vertexes.Add(new LwPolylineVertex(v27));
            line6.Vertexes.Add(new LwPolylineVertex(v26));
            line6.Vertexes.Add(new LwPolylineVertex(v35));
            line6.Vertexes.Add(new LwPolylineVertex(v34));
            line6.Vertexes.Add(new LwPolylineVertex(v25));
            line6.Vertexes.Add(new LwPolylineVertex(v26));
            line6.Vertexes[4].Bulge = Math.Tan(156 * pi / 180 / 4);
            fun.PolyFill(dxf, line6, 0.2, contour);

            LwPolyline line7 = new LwPolyline();
            line7.Vertexes.Add(new LwPolylineVertex(v25));
            line7.Vertexes.Add(new LwPolylineVertex(v24));
            line7.Vertexes.Add(new LwPolylineVertex(v33));
            line7.Vertexes.Add(new LwPolylineVertex(v32));
            line7.Vertexes.Add(new LwPolylineVertex(v23));
            line7.Vertexes.Add(new LwPolylineVertex(v24));
            line7.Vertexes[4].Bulge = Math.Tan(156 * pi / 180 / 4);
            fun.PolyFill(dxf, line7, 0.2, contour);

            LwPolyline line8 = new LwPolyline();
            line8.Vertexes.Add(new LwPolylineVertex(v31));
            line8.Vertexes.Add(new LwPolylineVertex(v15));
            line8.Vertexes.Add(new LwPolylineVertex(v14));
            line8.Vertexes.Add(new LwPolylineVertex(v21));
            line8.Vertexes.Add(new LwPolylineVertex(v7));
            line8.Vertexes.Add(new LwPolylineVertex(v21));
            line8.Vertexes.Add(new LwPolylineVertex(v3));
            line8.Vertexes.Add(new LwPolylineVertex(v18));
            line8.Vertexes.Add(new LwPolylineVertex(v20));
            line8.Vertexes.Add(new LwPolylineVertex(v19));
            line8.Vertexes.Add(new LwPolylineVertex(v17));
            line8.Vertexes.Add(new LwPolylineVertex(v16));
            line8.Vertexes.Add(new LwPolylineVertex(v13));
            line8.Vertexes.Add(new LwPolylineVertex(v12));
            fun.PolyFill(dxf, line8, 0.2, contour);

            Line line9 = new Line(v45, v44);
            line9.Layer = refin;
            line9.Linetype = Linetype.Dashed;
            line9.LinetypeScale = 5;
            dxf.AddEntity(line9);

            Line line10 = new Line(v44, v47);
            line10.Layer = refin;
            line10.Linetype = Linetype.Dashed;
            line10.LinetypeScale = 5;
            dxf.AddEntity(line10);

            Line line11 = new Line(v45, v46);
            line11.Layer = refin;
            line11.Linetype = Linetype.Dashed;
            line11.LinetypeScale = 5;
            dxf.AddEntity(line11);

            Line line12 = new Line(v46, v47);
            line12.Layer = refin;
            line12.Linetype = Linetype.Dashed;
            line12.LinetypeScale = 5;
            dxf.AddEntity(line12);

            //2
            Line line13 = new Line(v48, v49);
            line13.Layer = centre;
            line13.Linetype = Linetype.DashDot;
            line13.LinetypeScale = 3;
            dxf.AddEntity(line13);

            Line line14 = new Line(v68, v69);
            line14.Layer = refin;
            line14.Linetype = Linetype.Dashed;
            line14.LinetypeScale = 5;
            dxf.AddEntity(line14);
           
            Line line15 = new Line(v69, v70);
            line15.Layer = refin;
            line15.Linetype = Linetype.Dashed;
            line15.LinetypeScale = 5;
            dxf.AddEntity(line15);

            Line line16 = new Line(v70, v71);
            line16.Layer = refin;
            line16.Linetype = Linetype.Dashed;
            line16.LinetypeScale = 5;
            dxf.AddEntity(line16);

            Line line17 = new Line(v71, v68);
            line17.Layer = refin;
            line17.Linetype = Linetype.Dashed;
            line17.LinetypeScale = 5;
            dxf.AddEntity(line17);

            LwPolyline line18 = new LwPolyline();
            line18.Vertexes.Add(new LwPolylineVertex(v54));
            line18.Vertexes.Add(new LwPolylineVertex(v53));
            line18.Vertexes.Add(new LwPolylineVertex(v51));
            line18.Vertexes.Add(new LwPolylineVertex(v52));
            line18.Vertexes.Add(new LwPolylineVertex(v50));
            line18.Vertexes.Add(new LwPolylineVertex(v62));
            fun.PolyFill(dxf, line18, 0.2, contour);

            LwPolyline line19 = new LwPolyline();
            line19.Vertexes.Add(new LwPolylineVertex(v65));
            line19.Vertexes.Add(new LwPolylineVertex(v64));
            line19.Vertexes.Add(new LwPolylineVertex(v67));
            line19.Vertexes.Add(new LwPolylineVertex(v66));
            line19.Vertexes.Add(new LwPolylineVertex(v63));
            line19.Vertexes.Add(new LwPolylineVertex(v64));
            line19.Vertexes[4].Bulge = Math.Tan(-156 * pi / 180 / 4);
            fun.PolyFill(dxf, line19, 0.2, contour);

            LwPolyline line20 = new LwPolyline();
            line20.Vertexes.Add(new LwPolylineVertex(v63));
            line20.Vertexes.Add(new LwPolylineVertex(v61));
            line20.Vertexes.Add(new LwPolylineVertex(v59));
            line20.Vertexes.Add(new LwPolylineVertex(v60));
            line20.Vertexes.Add(new LwPolylineVertex(v59));
            line20.Vertexes.Add(new LwPolylineVertex(v57));
            line20.Vertexes.Add(new LwPolylineVertex(v58));
            line20.Vertexes.Add(new LwPolylineVertex(v57));
            line20.Vertexes.Add(new LwPolylineVertex(v55));
            line20.Vertexes.Add(new LwPolylineVertex(v56));
            line20.Vertexes.Add(new LwPolylineVertex(v55));
            line20.Vertexes.Add(new LwPolylineVertex(v74));
            line20.Vertexes.Add(new LwPolylineVertex(v75));
            fun.PolyFill(dxf, line20, 0.2, contour);

            LwPolyline line21 = new LwPolyline();
            line21.Vertexes.Add(new LwPolylineVertex(v72));
            line21.Vertexes.Add(new LwPolylineVertex(v73));
            fun.PolyFill(dxf, line21, 0.05, dimensioning);


            //坐标系
            fun.Axis(dxf, new Vector2(v9.X, v2.Y), -90, 180, "", "", "PCS", 0.25, 2, 0);

            fun.Axis(dxf, v41, -90, 180, "", "", "MCS", 0.25, 2, 0);

            fun.Axis(dxf, v3, -90, 180, "", "", "CIP", 0.25, 2, 0);

            fun.Axis(dxf, v53, -90, 180, "X", "Y", "Z", 0.25, 2, 0);


            //标注

            double height = 2;
            double arrowsize = 2;
            double exline = 2;



            fun.dimrot1(dxf, v15, v10, 180, oal / 3, 1, height, arrowsize, exline, "OAL", "", "");

            fun.dimrot1(dxf, v43, v10, 180, oal / 5, 1, height, arrowsize, exline, "LPR", "", "");

            fun.dimrot1(dxf, v9, v4, 0, oal / 4, 1, height, arrowsize, exline, "LF", "", "");

            fun.dimrot1(dxf, v42, v4, 0, oal / 7, 1, height, arrowsize, exline, "LTA", "", "");

            fun.dimrot1(dxf, v5, v4, 0, oal / 7 - (v5.Y + v4.Y) / 2 + (v42.Y + v4.Y) / 2, 1, height, arrowsize, exline, "LDC", "", "");

            fun.dimrot1(dxf, v8, v11, 90, dconms, 1, height, arrowsize, exline, "DCONMS", "", "");

            fun.dimrot1(dxf, v5, v15, -90, dconms * 2, 1, height, arrowsize, exline, "DTAN", "", "");

            fun.dimrot1(dxf, v4, new Vector2(v4.X, v4.Y * -1), -90, dconms * 4, 1, height, arrowsize, exline, "DC", "", "");

            fun.dimrot1(dxf, v6, v14, 90, dconms, 1, height, arrowsize, exline, "DTAX", "", "");

            fun.jdbz2(dxf, new Line(v6, fun.Polar(v6, (180 - prfa / 2) * pi / 180, 0.1)), new Line(v14, fun.Polar(v14, (prfa / 2 - 180) * pi / 180, 0.1)), oal * 3, height, arrowsize, exline, "PRFA", "", "");

            fun.jt(dxf, new Vector2(v1.X + oal / 2.589, v1.Y - dc / 0.266), 180, 9, height, "FDP");

            fun.jt(dxf, new Vector2(v41.X - 2, v8.Y), 135, 9, height, "CCMS");

            fun.text1(dxf, new Vector2(v73.X + oal / 66.463, v73.Y - dc / 2.102), 90, "CSW");








            //定义格式，路径导出
            dxf.DrawingVariables.AcadVer = DxfVersion.AutoCad2007;
            dxf.Save(path);


        }

    }
}